<template>
  <div class="search">
    <div class="bofanggequ"><FooterMusicVue></FooterMusicVue></div>

    <div class="searchTop">
      <svg class="icon icon-direction-left" aria-hidden="true" @click="back" >
        <use xlink:href="#icon-direction-left"></use>
      </svg>
      <input
        type="text"
        v-model="searchKey"
        placeholder="张杰"
        @keydown.enter="enderKey"
      
      />
      <!-- <van-search width="90%" v-model="value" placeholder="张杰" id="input"/> -->
    </div>
    <div class="searchHistory">
      <span class="searchspan">历史</span>
      <span
        class="spanKey"
        v-for="item in keyWordList"
        @click="searchHistor(item)"
        :key="item"
        >{{ item }}
      </span>

      <svg class="icon icon-ashbin" aria-hidden="true" @click="delHistory">
        <use xlink:href="#icon-ashbin"></use>
      </svg>
    </div>

    <div class="allite" v-for="(item, i) in searchList" :key="i">
      <div class="iteLis" :singer-data="item" :is-play-icon="true">
        <div class="item" @click="updateIndex(item)">
          <div class="theitemLeft">
            <span class="leftSpan">{{ i + 1 }} </span>
          </div>
          <div class="musicsong">
            <p>{{ item.name }}</p>

            <span v-for="(itemone, index) in item.artists" :key="index">
              {{ itemone.name }}
            </span>
          </div>
        </div>
      </div>
      <div class="theitemRight">
        <div>
          <svg class="icon bofang" aria-hidden="true" v-if="item.mvid != 0">
            <!-- <use
              :xlink:href="[isPlay ? '#icon-zanting' : '#icon-bofang']"
            ></use> -->
            <use xlink:href="#icon-play"></use>
          </svg>
        </div>
        <svg class="icon liebiao" aria-hidden="true">
          <use xlink:href="#icon-bofanggedan"></use>
        </svg>
      </div>
    </div>
  </div>
</template>

<script>
// import FooterMusicVue from './MusicDetail.vue'

import FooterMusicVue from "../components/item/FooterMusic.vue";

// import { mapMutations, mapState } from 'vuex';
export default {
  name: "Search",

  components: {
    FooterMusicVue,
  },

  data() {
    return {
      keyWordList: [],
      searchKey: "",
      //搜索结果数据
      searchworld: [],
      //搜索列表接受数据
      searchList: [],
    };
  },
  created() {
    //   this.getSearchtheMusic((this.keyWordList))
    this.getSearchtheMusic(this.keyWordList);
  },
  methods: {
        
      
        back:function() {
      let isback = true
      if (isback) {
        this.$children[0].backplay()
       
      }else{
        
      } 
      this.$router.back();
    },
 


    enderKey: function () {
      if (this.searchKey != "") {
        //添加到前面
        this.keyWordList.unshift(this.searchKey);
        //去重
        this.keyWordList = [...new Set(this.keyWordList)];
        //规定长度 多了就删除
        if (this.keyWordList.length > 10) {
          this.keyWordList.splice(this.keyWordList.length - 1, 1);
        }
        localStorage.setItem("keyWordList", JSON.stringify(this.keyWordList));

        this.getSearchtheMusic(this.keyWordList);

        // this.searchList = res.data.result.songs
        this.searchKey = "";
          
      }
       
    },
    delHistory: function () {
      localStorage.removeItem("keyWordList");
      this.keyWordList = [];
    },

    updateIndex: function (item) {
      item.al = item.album;
      item.al.picUrl = item.album.artist.img1v1Url;
      item.al.name = item.name;
      // item.al.name = item.name
      //进行提交使用pushPlayList函数
      this.$store.commit("pushPlayList", item);
      //播放push到最后的一首歌
      this.$store.commit(
        "updatePlayListIndex",
        this.$store.state.playList.length - 1
      );
      
    },

    searchHistor: function (item) {
      this.getSearchtheMusic(item);
    //   this.updateIndex(item);

    //   this.searchList = res.data.result.songs
    },

    //申请歌词
    getSearchtheMusic(datas) {
      this.axios({
        method: "get",
        url: `/search?keywords=${datas}`,
        params: {
           id:33894312
        },
      })
        .then((res) => {
          //  console.log("res==>",res);

          this.searchworld = res.data.result;
          console.log("搜索建议==>", this.searchworld);

          this.searchList = res.data.result.songs;
          console.log("真正的历史搜索==>", this.searchList);
        })
        .catch((err) => {
          console.log("err==>", err);
        });
    },
  },

  mounted() {
    this.keyWordList = JSON.parse(localStorage.getItem("keyWordList"))
      ? JSON.parse(localStorage.getItem("keyWordList"))
      : [];
  },
};
</script>

<style lang="less" scoped>
.search {
  margin: 10px 15px 0 15px;
  .bofanggequ {
    position: absolute;
    left: 0px;
  }
  .searchTop {
    width: 100%;
    height: 50px;

    display: flex;
    align-items: center;
    input {
      margin-left: 10px;
      border: none;
      border-bottom: 2px solid rgb(172, 170, 170);
      width: 90%;
      padding: 5px;
    }
  }
  .searchHistory {
    width: 100%;
    display: flex;
    align-items: flex-end;
    // justify-content: center;
    flex-wrap: wrap;

    position: relative;
    .searchspan {
      font-weight: 700;
      line-height: 40px;
    }
    .spanKey {
      padding: 5px 10px;
      background-color: rgb(186, 184, 184);
      margin: 5px 10px;
      border-radius: 20px;
    }
    .icon-ashbin {
      width: 15px;
      height: 15px;
      position: absolute;
      right: 0px;
    }
  }

  .allite {
    // width: 100%;
    display: flex;
    align-items: center;
    justify-content: space-between;
    .iteLis {
      width: 83%;
      // height: 60px;
      display: flex;
      align-items: center;
      justify-content: space-between;
      .item {
        margin-left: 10px;
        display: flex;
        align-items: center;
        .theitemLeft {
          .leftSpan {
            font-size: medium;
            font-weight: 600;
            margin-right: 10px;
            color: #4d4d4e;
          }
        }
        .musicsong {
          word-wrap: break-word;
          *word-break: break-all;
          width: 220px;

          p {
            white-space: pre-wrap;
            width: 210px;
            //  width: 80%;
            //有特殊字符只显示一行
            word-wrap: break-word;
            font-size: medium;
            font-weight: 900;
            margin: 6px 0;
          }
          span {
            font-size: small;
            color: #bdbdbd;
          }
        }
      }
    }
    .theitemRight {
      margin-right: 10px;
      display: flex;
      align-items: center;
      .bofang {
        margin-right: 10px;
      }
    }
  }
}
</style>
